﻿using Red.Controls.Diagrams.ViewModels;
using Red.Controls.ViewModels;
using System.Reflection;

namespace Red.Toolkit.NodeDeclaratorEditor
{
    public class DummyOutputConnectorPropertyDataItem : DataItem
    {
        #region Data

        private readonly DummyConnectorViewModel _connector;
        private readonly PropertyInfo _propertyInfo;

        #endregion Data

        #region C-tor

        public DummyOutputConnectorPropertyDataItem( DataItem parent, DummyConnectorViewModel connector, string propertyName )
            : base( parent )
        {
            _connector = connector;
            Title = propertyName;
            IsReadOnly = _connector.IsInherited;
            _propertyInfo = connector.GetType().GetProperty( propertyName );
            IsExpanded = true;
        }

        #endregion C-tor

        #region DataItem implementation

        public override string Type => "String";

        public override string Value
        {
            get
            {
                var value = _propertyInfo.GetValue( _connector );
                return value != null ? value.ToString() : "";
            }
            set
            {
                _propertyInfo.SetValue( _connector, value );
            }
        }

        public override DataItemKind Kind => DataItemKind.Simple;

        #endregion DataItem implementation
    }
}
